// LemCoop
// Copyright (C) 2013  Oliver Naumann <bldrax@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "logmanager.h"

std::ofstream LogManager::m_out;

std::string LogManager::CreateTimestamp(std::string date_separator, std::string date_time_separator, std::string time_separator)
{
	char buffer[80];
	time_t logtime;

	time(&logtime);

	tm timeinfo;
	localtime_s(&timeinfo, &logtime);

	std::string timestampPattern = "%Y" + date_separator + "%m" + date_separator + "%d" + date_time_separator + "%H" + time_separator + "%M" + time_separator + "%S";

	strftime(buffer, 80, timestampPattern.c_str(), &timeinfo);

	return std::string(buffer);
}

void LogManager::Initialize(std::string file_prefix)
{
	std::string filename = file_prefix + CreateTimestamp("_", "_", "_") + ".log";

	m_out.open(filename.c_str());
}

void LogManager::LogLine(std::string message)
{
	m_out << "[" << CreateTimestamp("/", " ", ":") << "] " << message << std::endl;
}

void LogManager::Close()
{
	m_out.close();
}